﻿package mortal.game.view.mining
{
    import Message.Game.*;
    import Message.Public.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.events.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.view.mining.panel.*;
    import mortal.game.view.task.data.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class MiningController extends Controller
    {
        private var _module:MiningModule;
        private var _moduleSub:MiningRefleshPanel;
        private var _helpPanel:MiningHelpPanel;

        public function MiningController()
        {
            return;
        }// end function

        override protected function initServer() : void
        {
            super.initServer();
            NetDispatcher.addCmdListener(ServerCommand.MiningTaskInfosGot, this.taskInfosGot);
            NetDispatcher.addCmdListener(ServerCommand.MiningMemberInfosGot, this.memberInfosGot);
            NetDispatcher.addCmdListener(ServerCommand.MiningBeAskHelp, this.beAskHelpHandler);
            NetDispatcher.addCmdListener(ServerCommand.TaskDoingAdd, this.taskDoingAddHandler);
            Dispatcher.addEventListener(EventName.MiningShowModule, this.showModuleHandler);
            Dispatcher.addEventListener(EventName.MiningRefuseHelp, this.refuseHelpHandler);
            Dispatcher.addEventListener(EventName.MiningRefuseAllAskHelp, this.refuseAllAskHelp);
            Dispatcher.addEventListener(EventName.ActiveStart, this.activeStartHandler);
            Dispatcher.addEventListener(EventName.ActiveEnd, this.activeEndHandler);
            Dispatcher.addEventListener(EventName.ActiveNotic, this.activesNoticHandler);
            return;
        }// end function

        private function activesNoticHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SClientActive;
            if (_loc_2.type != EActiveType._EActiveTypeRoutine)
            {
                return;
            }
            if (cache.role.roleInfo.level < GameDefConfig.instance.getSystemOpenLevel(43))
            {
                return;
            }
            var _loc_3:* = Math.ceil((_loc_2.startDt.time - ClockManager.instance.nowDate.time) / 1000);
            if (_loc_3 <= 0)
            {
                return;
            }
            if (cache.task.getGroupFinishCount(ETaskGroup._ETaskGroupMinning) >= 3)
            {
                if (MiningIcon.hasInstance && !MiningIcon.instance.isHide)
                {
                    MiningIcon.instance.hide();
                }
                return;
            }
            MiningIcon.instance.show();
            MiningIcon.instance.setLeftTimeAndState(_loc_3, 1);
            return;
        }// end function

        private function activeEndHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SClientActive;
            if (_loc_2.type != EActiveType._EActiveTypeRoutine)
            {
                return;
            }
            if (this._module != null && !this._module.isHide)
            {
                this._module.hide();
            }
            if (MiningIcon.hasInstance)
            {
                MiningIcon.instance.hide();
            }
            return;
        }// end function

        private function activeStartHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SClientActive;
            if (_loc_2.type != EActiveType._EActiveTypeRoutine)
            {
                return;
            }
            if (cache.role.roleInfo.level < GameDefConfig.instance.getSystemOpenLevel(43))
            {
                return;
            }
            var _loc_3:* = Math.ceil((_loc_2.endDt.time - ClockManager.instance.nowDate.time) / 1000);
            if (_loc_3 <= 0)
            {
                return;
            }
            if (MiningIcon.instance.isHide)
            {
                this.showModule(true);
                (this.view as MiningModule).isDouble = true;
            }
            MiningIcon.instance.show();
            MiningIcon.instance.setLeftTimeAndState(_loc_3, 2);
            return;
        }// end function

        private function taskDoingAddHandler(param1) : void
        {
            var _loc_2:* = int(param1);
            var _loc_3:* = cache.task.getTaskByCode(_loc_2);
            if (_loc_3 == null)
            {
                return;
            }
            if (_loc_3.stask.group == ETaskGroup._ETaskGroupMinning)
            {
            }
            return;
        }// end function

        private function showModuleHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as Boolean;
            this.showModule(_loc_2);
            return;
        }// end function

        public function showModule(param1:Boolean) : void
        {
            var _loc_2:TaskInfo = null;
            var _loc_3:int = 0;
            if (param1)
            {
                if (!cache.guild.hasGuild)
                {
                    MsgManager.showRollTipsMsg(Language.getString(21041));
                    return;
                }
                this.view.show();
                if (this._moduleSub == null)
                {
                    this._moduleSub = UICompomentPool.getUICompoment(MiningRefleshPanel) as MiningRefleshPanel;
                }
                this._moduleSub.layer = this._module;
                this._moduleSub.show(773, 0);
                GameProxy.task.getMiningRefreshInfos();
                _loc_2 = cache.mining.getMiningTask();
                _loc_3 = 0;
                if (_loc_2 != null)
                {
                    _loc_3 = _loc_2.stask.code;
                }
                GameProxy.task.getMiningAllTaskInfos(_loc_3);
                this.updateTask(null);
            }
            else if (this._module != null && !this._module.isHide)
            {
                this._module.hide();
            }
            return;
        }// end function

        private function beAskHelpHandler(param1:Object) : void
        {
            if (MiningHintIcon.instance.isHide && cache.mining.getBeAskHelpDataProvider().length > 0)
            {
                MiningHintIcon.instance.show();
            }
            if (this._helpPanel != null && !this._helpPanel.isHide)
            {
                this._helpPanel.updateAllDatas();
            }
            return;
        }// end function

        private function refuseAllAskHelp(event:DataEvent) : void
        {
            cache.mining.clearBeAskHelp();
            if (this._helpPanel != null)
            {
                this._helpPanel.hide();
            }
            MiningHintIcon.instance.hide();
            return;
        }// end function

        private function refuseHelpHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as STaskRoutineTaskOtherPlayersInfo;
            if (_loc_2 == null)
            {
                return;
            }
            cache.mining.delBeAskHelp(_loc_2);
            if (this._helpPanel != null && !this._helpPanel.isHide)
            {
                this._helpPanel.updateAllDatas();
            }
            return;
        }// end function

        private function taskInfosGot(param1:Object) : void
        {
            this.updateTask(null);
            return;
        }// end function

        private function memberInfosGot(param1:Object) : void
        {
            this.updateTask(null);
            if (this._helpPanel != null && !this._helpPanel.isHide)
            {
                this._helpPanel.updateAllDatas();
            }
            return;
        }// end function

        override public function get view() : IView
        {
            if (this._module == null)
            {
                this._module = UICompomentPool.getUICompoment(MiningModule) as MiningModule;
                this._module.addEventListener(Event.ADDED_TO_STAGE, this.moduleAddHandler);
                this._module.addEventListener(Event.REMOVED_FROM_STAGE, this.moduleRemovedHandler);
            }
            return this._module;
        }// end function

        private function moduleRemovedHandler(event:Event) : void
        {
            NetDispatcher.removeCmdListener(ServerCommand.TaskUpdate, this.updateTask);
            NetDispatcher.removeCmdListener(ServerCommand.TaskDoingAdd, this.updateTask);
            NetDispatcher.removeCmdListener(ServerCommand.TaskDoingDel, this.updateTask);
            NetDispatcher.removeCmdListener(ServerCommand.TaskCanGetAdd, this.updateTask);
            NetDispatcher.removeCmdListener(ServerCommand.TaskCanGetDel, this.updateTask);
            NetDispatcher.removeCmdListener(ServerCommand.TaskCanGetRecv, this.updateTask);
            NetDispatcher.removeCmdListener(ServerCommand.TaskDoingListRecv, this.updateTask);
            if (this._moduleSub != null)
            {
                this._moduleSub.hide();
            }
            return;
        }// end function

        private function moduleAddHandler(event:Event) : void
        {
            NetDispatcher.addCmdListener(ServerCommand.TaskUpdate, this.updateTask);
            NetDispatcher.addCmdListener(ServerCommand.TaskDoingAdd, this.updateTask);
            NetDispatcher.addCmdListener(ServerCommand.TaskDoingDel, this.updateTask);
            NetDispatcher.addCmdListener(ServerCommand.TaskCanGetAdd, this.updateTask);
            NetDispatcher.addCmdListener(ServerCommand.TaskCanGetDel, this.updateTask);
            NetDispatcher.addCmdListener(ServerCommand.TaskCanGetRecv, this.updateTask);
            NetDispatcher.addCmdListener(ServerCommand.TaskDoingListRecv, this.updateTask);
            return;
        }// end function

        public function updateTask(param1:Object) : void
        {
            if (this._module != null && !this._module.isHide)
            {
                this._module.updateAllInfos();
            }
            if (this._moduleSub != null && !this._moduleSub.isHide)
            {
                this._moduleSub.updateAllInfos();
            }
            return;
        }// end function

        public function showHelpPanel() : void
        {
            if (this._helpPanel == null)
            {
                this._helpPanel = UICompomentPool.getUICompoment(MiningHelpPanel) as MiningHelpPanel;
            }
            this._helpPanel.show();
            if (!this._helpPanel.updateAllDatas())
            {
                MsgManager.showRollTipsMsg(Language.getString(21042));
            }
            return;
        }// end function

    }
}
